package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.user.UserDO;
import com.qk.dto.user.UserDTO;
import com.qk.entity.User;
import com.qk.management.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: zyx
 * @created: 2025-10-07-19:35
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    public Result page(UserDTO userDto) {
        log.info("分页查询用户,参数：用户名={},状态={},手机={},部门={},页码={},每页大小={}", userDto.getName(), userDto.getStatus(), userDto.getPhone(), userDto.getDeptId(), userDto.getPage(), userDto.getPageSize());
        PageResult<UserDO> pageResult = userService.page(userDto.getName(), userDto.getStatus(), userDto.getPhone(), userDto.getDeptId(), userDto.getPage(), userDto.getPageSize());
        return Result.success(pageResult);
    }

    @PostMapping
    public Result addUser(@RequestBody User user) {
        log.info("新增用户,参数：{}", user);
        userService.addUser(user);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getUserById(@PathVariable("id") Integer id) {
        log.info("根据ID查询用户: {}", id);
        User user = userService.getUserById(id);
        return Result.success(user);
    }
    @PutMapping
    public Result updateUser(@RequestBody User user) {
        log.info("修改用户: {}", user);
        userService.updateUser(user);
        return Result.success();
    }

    @GetMapping("/role/{roleLabel}")
    public Result listByRole(@PathVariable("roleLabel") String roleLabel) {
        log.info("根据角色查询用户: {}", roleLabel);
        List<User> users = userService.listByRole(roleLabel);
        return Result.success(users);
    }
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable("ids") List<Integer> ids) {
        log.info("批量删除用户: {}", ids);
        userService.deleteUsers(ids);
        return Result.success();
    }
}
